<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/25
 * Time: 14:42
 */
namespace app\shop\controller;
use think\Db;
class Index extends Base
{
    public function index(){
        $this->redirect (url('shop/Task/homePage'));
    }
    public function tongji(){
        extract(input());
        $user_id = session('shop_id');
        if(isset($start) && $start && isset($end) && $end){
            $data = $this->getEchartsDataHaveDate($user_id,$start,$end);
        }else {
            $start = '';
            $end = '';
            $all = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_num');
            //领取的
            $all_get = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_get_num');
            //未领取的
            $all_not_get = $all - $all_get;

            //已核销的
            $all_use = Db::table('up_shop_coupon')
                ->where('coupon_s_id', $user_id)
                ->sum('coupon_use_num');
            //未核销的
            $all_not_use = $all_get - $all_use;

            //所有的任务
            $all_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->sum('task_num');
            //所有的普通任务
            $all_pt_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_num');
            //所有的活动任务
            $all_hd_task = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_num');
            //所有的达人任务
            $all_dr_task = $all_task - $all_pt_task - $all_hd_task;

            //普通已领取
            $pt_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_get_num');
            //普通待领取
            $pt_not_get = $all_pt_task - $pt_get;
            //普通已完成审核
            $pt_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 1)
                ->sum('task_done_num');
            //普通待审核
            $pt_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 1)
                ->where('tu_status', 1)
                ->count();

            //活动已领取
            $hd_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_get_num');
            //活动待领取
            $hd_not_get = $all_hd_task - $hd_get;
            //活动已完成审核
            $hd_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 2)
                ->sum('task_done_num');
            //活动待审核
            $hd_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 2)
                ->where('tu_status', 1)
                ->count();

            //活动已领取
            $dr_get = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 3)
                ->sum('task_get_num');
            //活动待领取
            $dr_not_get = $all_dr_task - $dr_get;
            //活动已完成审核
            $dr_use = Db::table('up_task')
                ->where('task_s_id', $user_id)
                ->where('task_active', 3)
                ->sum('task_done_num');
            //活动待审核
            $dr_not_use = Db::table('up_task_user')
                ->where('tu_s_id', $user_id)
                ->where('tu_task_active', 3)
                ->where('tu_status', 1)
                ->count();

            $data = [
                'all_get' => $all_get,
                'all_not_get' => $all_not_get,

                'all_use' => $all_use,
                'all_not_use' => $all_not_use,

                'all_task' => $all_task,
                'all_pt_task' => $all_pt_task,
                'all_hd_task' => $all_hd_task,
                'all_dr_task' => $all_dr_task,

                'pt_get' => $pt_get,
                'pt_not_get' => $pt_not_get,
                'pt_use' => $pt_use,
                'pt_not_use' => $pt_not_use,

                'hd_get' => $hd_get,
                'hd_not_get' => $hd_not_get,
                'hd_use' => $hd_use,
                'hd_not_use' => $hd_not_use,

                'dr_get' => $dr_get,
                'dr_not_get' => $dr_not_get,
                'dr_use' => $dr_use,
                'dr_not_use' => $dr_not_use,
            ];
        }
        $data['hd_get_bai'] = 0;
        $data['hd_not_get_bai'] = 0;
        $all_hd_task = $data['hd_get'] + $data['hd_not_get'];
        if($all_hd_task > 0){
            $data['hd_get_bai'] = round(($data['hd_get']/$all_hd_task)*100,2);
            $data['hd_not_get_bai'] = round(($data['hd_not_get']/$all_hd_task)*100,2);
        }
        $data['hd_use_bai'] = 0;
        $data['hd_not_use_bai'] = 0;
        $hd_all_use = $data['hd_use'] + $data['hd_not_use'];
        if($hd_all_use > 0){
            $data['hd_use_bai'] = round(($data['hd_use']/$hd_all_use)*100,2);
            $data['hd_not_use_bai'] = round(($data['hd_not_use']/$hd_all_use)*100,2);
        }
        $this->assign('data',$data);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch ('index/tongji');
    }
    public function getEchartsDataHaveDate($user_id,$start,$end)
    {
        extract(input());
        $all = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_num');
        //领取的
        $all_get = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_get_num');
        //未领取的
        $all_not_get = $all - $all_get;

        //已核销的
        $all_use = Db::table('up_shop_coupon')
            ->where('coupon_s_id',$user_id)
            ->whereTime('coupon_time','between',[$start,$end])
            ->sum('coupon_use_num');
        //未核销的
        $all_not_use = $all_get - $all_use;

        //所有的任务
        $all_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的普通任务
        $all_pt_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的活动任务
        $all_hd_task = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_num');
        //所有的达人任务
        $all_dr_task = $all_task - $all_pt_task - $all_hd_task;

        //普通已领取
        $pt_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //普通待领取
        $pt_not_get = $all_pt_task - $pt_get;
        //普通已完成审核
        $pt_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',1)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //普通待审核
        $pt_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',1)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        //活动已领取
        $hd_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //活动待领取
        $hd_not_get = $all_hd_task - $hd_get;
        //活动已完成审核
        $hd_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',2)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //活动待审核
        $hd_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',2)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        //活动已领取
        $dr_get = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',3)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_get_num');
        //活动待领取
        $dr_not_get = $all_dr_task - $dr_get;
        //活动已完成审核
        $dr_use = Db::table('up_task')
            ->where('task_s_id',$user_id)
            ->where('task_active',3)
            ->whereTime('task_time','between',[$start,$end])
            ->sum('task_done_num');
        //活动待审核
        $dr_not_use = Db::table('up_task_user')
            ->where('tu_s_id',$user_id)
            ->where('tu_task_active',3)
            ->where('tu_status',1)
            ->whereTime('tu_submit_time','between',[$start,$end])
            ->count();

        $data = [
            'all_get' => $all_get,
            'all_not_get' => $all_not_get,

            'all_use' => $all_use,
            'all_not_use' => $all_not_use,

            'all_task' => $all_task,
            'all_pt_task' => $all_pt_task,
            'all_hd_task' => $all_hd_task,
            'all_dr_task' => $all_dr_task,

            'pt_get' => $pt_get,
            'pt_not_get' => $pt_not_get,
            'pt_use' => $pt_use,
            'pt_not_use' => $pt_not_use,

            'hd_get' => $hd_get,
            'hd_not_get' => $hd_not_get,
            'hd_use' => $hd_use,
            'hd_not_use' => $hd_not_use,

            'dr_get' => $dr_get,
            'dr_not_get' => $dr_not_get,
            'dr_use' => $dr_use,
            'dr_not_use' => $dr_not_use,
        ];
        return $data;
    }
    public function changepass(){
        $phone = session('login_phone');
        $phone = substr($phone,0,3) . '****' . substr($phone,-4,4);
        $this->assign('phone',$phone);
        return $this->fetch ('index/changepass');
    }
    public function changephone(){
        $phone = session('login_phone');
        $phone = substr($phone,0,3) . '****' . substr($phone,-4,4);
        $this->assign('phone',$phone);
        return $this->fetch ('index/changephone');
    }
    /**
     * 修改密码
     * @return mixed|\think\response\Json
     */
    public function changePwd(){
        extract(input());
        $phone = session('login_phone');
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['code'=>0,'msg'=>'请先发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['code'=>0,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['code'=>0,'msg'=>'验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            $s_auth = session('shop_auth');
            if($s_auth == 200){
                Db::name('shops')->where(['s_phone'=>$phone])->update(['s_password'=>md5($pwd),'s_update_time'=>date('Y-m-d H:i:s')]);
            }else{
                Db::name('role_name')
                    ->where('role_user',$phone)->update([
                        'password' => $pwd,
                        'role_password' => md5($pwd),
                        'role_time' => date('Y-m-d H:i:s')
                    ]);
            }
            Db::commit();
            return json(['code'=>200,'url'=>url('shop/Index/tongji'),'msg'=>'修改成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code'=>100,'msg'=>'密码重置失败']);
        }
    }
    /**
     * 换手机号
     * @return mixed|\think\response\Json
     */
    public function changePho(){
        extract(input());
        $phone = session('login_phone');
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['code'=>0,'msg'=>'请先给原手机号发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 6000){
            return json(['code'=>0,'msg'=>'原手机号验证码失效']);
        }
        if($code != $d['code']){
            return json(['code'=>0,'msg'=>'原手机号验证码错误']);
        }

        $d = Db::name('shop_phonecode')->where(['phone'=>$pwd,'status'=>0])->find();
        if(empty($d)){
            return json(['code'=>1,'msg'=>'请先给新手机号发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 6000){
            return json(['code'=>1,'msg'=>'新手机号验证码失效']);
        }
        if($pwd2 != $d['code']){
            return json(['code'=>1,'msg'=>'新手机号验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            Db::name('shop_phonecode')->where(['phone'=>$pwd])->setField ('status',1);
            $s_auth = session('shop_auth');
            if($s_auth == 200){
                Db::name('shops')->where(['s_phone'=>$phone])->update([
                    's_phone'=>$pwd,
                    's_china'=>$ex,
                    's_update_time'=>date('Y-m-d H:i:s')
                ]);
            }else{
                Db::name('role_name')
                    ->where('role_user',$phone)->update([
                        'role_user' => $pwd,
                        'role_time' => date('Y-m-d H:i:s')
                    ]);
            }
            Db::commit();
            session('login_phone',$pwd);
            return json(['code'=>200,'url'=>url('shop/Index/tongji'),'msg'=>'修改成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            session('login_phone',$phone);
            return json(['code'=>100,'msg'=>'密码重置失败']);
        }
    }
}